package nancal.mp

import nancal.mp.db.es.esr
import nancal.mp.db.mongo.mor
import nancal.mp.db.mysql.dbr
import nancal.mp.util.JenkinsUtil
import nbcp.base.comm.JsonList
import nbcp.base.comm.JsonMap
import nbcp.base.enums.LogLevelScopeEnum
import nbcp.base.extend.ToJson
import nbcp.base.extend.usingScope
import nbcp.base.utils.SpringUtil
import nbcp.myoql.db.es.component.query
import nbcp.myoql.db.mongo.aggregate
import nbcp.myoql.db.redis.scanAllKeys
import nbcp.myoql.db.sql.query
import org.junit.jupiter.api.Test
import org.springframework.data.redis.core.ScanOptions
import org.springframework.data.redis.core.StringRedisTemplate


class TestSql : TestBase() {
    @Test
    fun abcfff() {
var content = """
Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/iam-admin-backend
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (iam管理端)
[Pipeline] sh
[Pipeline] deleteDir
[Pipeline] sh
2024-11-15 17:14:03下载指令...
﹍﹎﹍﹎﹍﹎﹎﹎
+ wget -q http://amy/api/mp-admin-api/ops/jenkins/cmd/file/673710d9ee364c37aa4a8ba6 -O ops-cmd.zip
+ unzip -q -o -d ./ ops-cmd.zip
+ dos2unix docker-git.sh main-var.sh main.sh ops-api.sh ops-deploy.sh ops-docker.sh ops-git.sh ops-mvn.sh ops-sys.sh start-java-1.8.sh start-java.sh sys-post-deploy.sh sys-pre-deploy.sh
+ pwd
+ tree -L 3 ./
/var/jenkins_home/workspace/iam-admin-backend
./
|-- Dockerfile
|-- docker-git.sh
|-
|-- sys-post-deploy.sh
`-- sys-pre-deploy.sh

3 directories, 19 files
﹊﹉﹊﹉﹊﹉﹊﹊
source main.sh
/var/jenkins_home/workspace/iam-admin-backend

2024-11-15 17:14:04  docker amy:8082/tools/bash-ops 执行 sys-init
/var/jenkins_home/workspace/iam-admin-backend

2024-11-15 17:14:05 执行初始化
﹍﹎﹍﹎﹍﹎﹎﹎
产品线: 公共服务 (-)
开发语言: java:11
K8s服务名: 
git分支: dev-yuxh
构建文件夹: admin/admin-api
注册中心: boot
注册中文名: 中台api
注册名: iam管理端
运行环境: boot - dc,dc-dev
集群名: default
docker镜像: amy:8082/java/iam-admin-backend:20241115.171404
git仓库: http://10.0.8.6:3000/boe/iam-api.git
git提交id: 
git提交时间: 
生成时间: 2024-11-15 17:14:05
﹊﹉﹊﹉﹊﹉﹊﹊
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (下载代码)
[Pipeline] sh
source main.sh 
/var/jenkins_home/workspace/iam-admin-backend
获取代码 http://37bb94eecad6196b1edf0f716b0434842ef03692@10.0.8.6:3000/boe/iam-api.git
 docker run --rm  --privileged=true  --add-host amy:10.0.8.6           -v /var/jenkins_home/workspace/iam-admin-backend:/code  -w /code          amy:8082/tools/bash-ops bash -c 'TZ=Asia/Shanghai ; source docker-git.sh ' 
git clone --depth=1  --single-branch -b dev-yuxh http://37bb94eecad6196b1edf0f716b0434842ef03692@10.0.8.6:3000/boe/iam-api.git
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :admin-api
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (推送镜像)
Stage "推送镜像" skipped due to earlier failure(s)
[Pipeline] getContext
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (部署到K8s)
Stage "部署到K8s" skipped due to earlier failure(s)
[Pipeline] getContext
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (检查启动)
Stage "检查启动" skipped due to earlier failure(s)
[Pipeline] getContext
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
"""

        var res = JenkinsUtil.analyse(content);

        println(res)

    }


    fun scan2(matchKey: String): Set<String>   {
        var redisTemplate = SpringUtil.getBean(StringRedisTemplate::class.java);

        redisTemplate.scanAllKeys("*", callback = {
            println(it);
            return@scanAllKeys true
        })

        return redisTemplate.execute { connection ->
//            var con = ( connection.nativeConnection as RedisAdvancedClusterAsyncCommands<*,*>)
//            (con.statefulConnection as StatefulRedisClusterConnection).partitions
            val keys: MutableSet<String> = HashSet()
            val cursor = connection.scan(ScanOptions.scanOptions()
                    .match("$matchKey*").count(30).build())
            while (cursor.hasNext()) {
                keys.add(String(cursor.next()))
            }
            keys
        }
    }

//    @Test
//    fun cqq() {
//        var list = esr.system.appLogIndex.query()
//                .must({ it.content.match("mp") })
//                .limit(0, 10)
//                .toList()
//
//
//        println(list.size)
//        if (list.size > 0) {
//            println(list.first().group + ":" + list.first().content)
//        }
//    }

    @Test
    fun c() {
        usingScope(LogLevelScopeEnum.DEBUG) {

            var list = mor.dev.gitData.aggregate()
                    .beginMatch()
                    .where { it.language mongoEquals "Java" }
                    .endMatch()
                    .groupWithId(JsonMap("_id" to JsonMap("code" to "\$productLine.code", "name" to "\$productLine.name")))
                    .toMapList();

            println(list.size)

            list.forEach {
                println(it.ToJson())
            }
        }
    }

    @Test
    fun r() {
        dbr.base.s_annex.query()
                .toList()
                .forEach {
                    println(it.ToJson())
                }
    }

    @Test
    fun r_contains() {
        dbr.base.s_annex.query()
                .where { it.tags.sqlJsonArrayContains("c") }
                .toList()
                .forEach {
                    println(it.ToJson())
                }
    }


    @Test
    fun u() {
        dbr.base.s_annex.updateById("5qn7ur2j69ds")
                .set { it.tags to listOf("c", "d") }
                .exec()

        println(dbr.affectRowCount)
    }

    @Test
    fun u2() {
        dbr.base.s_annex.updateById("5qn7ur2j69ds")
                .set { it.name to "ffff" }
                .where { it.tags sqlEquals  JsonList.of("c", "d") }
                .exec()

        println(dbr.affectRowCount)

        dbr.base.s_annex.updateById("5qn7ur2j69ds")
                .set { it.name to "ffff" }
                .where { it.tags sqlEquals JsonList.of("d", "c") }
                .exec()

        println(dbr.affectRowCount)
    }


    @Test
    fun u3() {
        dbr.base.s_annex.updateById("5qn7ur2j69ds")
                .set { it.name to "ffff" }
                .where { it.tags sqlArrayContentEquals  JsonList.of("d", "c") }
                .exec()

        println(dbr.affectRowCount)
    }

}